<style>
    .avatar {
        width: 30px;
        height: 30px;
    }
</style>
<div class="widget-head" style="margin-top:0;">
    <div class="widget-title">数据库备份</div>
</div>
<div class="layui-container" style="width:100%;">
    <div class="layui-clear">
        <a class="layui-btn ajax-get shuaxin fl" data-msg="确定备份吗？" data-url="/system/api_admin/add_backup">备份数据库</a>
    </div>
    <table id="list"></table>
    <script type="text/html" id="barDemo">
        <div class="action-btn">
            <a target="_blank" href="/backup/{{d.file_name}}">下载</a>
            <a class="mini_del" data-url="/system/api_admin/del_backup/id/{{d.id}}">删除</a>
        </div>
    </script>
</div>

<script>
    layui.use(['jquery', 'think', 'table'], function() {
        var think = layui.think,
            $ = layui.jquery,
            table = layui.table;
        //第一个实例
        think.render({
                id: 'question-list',
                elem: '#list',
                method: 'post',
                skin: 'line',
                url: '/system/api_admin/getdata_backup',
                // where: condition,
                page: true,
                size: 'lg',
                // toolbar: true,
                cols: [
                    [ //表头
                        {
                            type: 'checkbox'
                        }, {
                            field: 'id',
                            title: 'ID',
                            width: 90
                        }, {
                            field: 'file_name',
                            title: '备份文件名'
                        }, {
                            title: '大小',
                            templet: function(d) {
                                return sizeTostr(d.file_size);
                            }
                        }, {
                            title: '头像',
                            width: 60,
                            templet: function(d) {
                                return d.avatar == '' || d.avatar == null ? '<img src="/Public/common/img/boy1.png" class="avatar">' : '<img src="' + d.avatar + '" class="avatar">';
                            }
                        }, {
                            field: 'realname',
                            title: '管理员'
                        }, {
                            field: 'create_time',
                            title: '创建时间',
                            width: 177,
                            templet: function(d) {
                                return think_time(d.create_time, 'Y-m-d H:i:s');
                            }
                        }, {
                            title: '操作',
                            width: 120,
                            align: 'center',
                            toolbar: '#barDemo'
                        }
                    ]
                ]
            },
            '{$_GET["searchtype"]}',
            '{$_GET["keywords"]}');
    });

    //转换文件大小
    function sizeTostr(size) {
        var data = "";
        if (size < 0.1 * 1024) { //如果小于0.1KB转化成B  
            data = size.toFixed(2) + "B";
        } else if (size < 0.1 * 1024 * 1024) { //如果小于0.1MB转化成KB  
            data = (size / 1024).toFixed(2) + "KB";
        } else if (size < 0.1 * 1024 * 1024 * 1024) { //如果小于0.1GB转化成MB  
            data = (size / (1024 * 1024)).toFixed(2) + "MB";
        } else { //其他转化成GB  
            data = (size / (1024 * 1024 * 1024)).toFixed(2) + "GB";
        }
        var sizestr = data + "";
        var len = sizestr.indexOf("\.");
        var dec = sizestr.substr(len + 1, 2);
        if (dec == "00") { //当小数点后为00时 去掉小数部分  
            return sizestr.substring(0, len) + sizestr.substr(len + 3, 2);
        }
        return sizestr;
    }
</script>